window.onload = function () {
  Get_Known_WIFI_SSID();
  Get_Connected_WIFI_SSID();
}

function Get_Known_WIFI_SSID() {
  var xhttp = new XMLHttpRequest();
  var Post_String = "password=" + document.getElementById("password").value;
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      var  Res_Text = this.responseText;
      var json_obj = eval( Res_Text);
      var Output_HTML = "已知WIFI:";
      //			document.getElementById("DHT_Value").innerHTML = Res_Test;
      for (var i in json_obj) {
        Output_HTML += "<div>WIFI_SSID:";
        Output_HTML += json_obj[i];
        Output_HTML += "<input type=\"submit\" formaction=\"./setting_page\" formmethod=\"POST\" title=\"设置\" value=\"删除\" onClick=\"WIFI_Change('delete',"+i+")\">";
        Output_HTML += "</div>";
      }
      document.getElementById("Known_WIFI_SSID").innerHTML = Output_HTML;
    }
  };
  xhttp.open("POST", "/Get_WIFI_SSIDs", true);
  xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhttp.send(Post_String);
  window.setTimeout(Get_Known_WIFI_SSID, 3000);
}

function Get_Connected_WIFI_SSID() {
  var xhttp = new XMLHttpRequest();
  var Post_String = "password=" + document.getElementById("password").value;
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      var  Res_Text = this.responseText;
      var json_obj = eval( Res_Text);
      var Output_HTML = "WIFI连接信息：";
      //			document.getElementById("DHT_Value").innerHTML = Res_Test;
      Output_HTML += "<div>已连接WIFI:" + json_obj[0] + "</div>";
      Output_HTML += "<div>该WIFI下的IP:" + json_obj[1] + "</div>";
      if (json_obj[1] == "(IP unset)") {
        Output_HTML = "WIFI未连接";
      }
      document.getElementById("Connected_WIFI_SSID").innerHTML = Output_HTML;
    }
  };
  xhttp.open("POST", "/Get_Connected_WIFI_Info", true);
  xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhttp.send(Post_String);
  window.setTimeout(Get_Known_WIFI_SSID, 3000);
}

function WIFI_Change(WIFI_Method, Position_Order) {
  var Post_String = "password=" + document.getElementById("password").value + "&";
		Post_String += "WIFI_Method=" +WIFI_Method + "&";
  if (WIFI_Method == "add") {
    Post_String += "WIFI_SSID=" + document.getElementById("WIFI_SSID").value + "&";
    Post_String += "WIFI_Password=" + document.getElementById("WIFI_Password").value + "&";
  } else if (WIFI_Method == "delete") {
    Post_String += "Position_Order=" + Position_Order + "";
  } else if (WIFI_Method == "change") {
    Post_String += "WIFI_SSID=" + document.getElementById("WIFI_SSID").value + "&";
    Post_String += "WIFI_Password=" + document.getElementById("WIFI_Password").value + "&";
    Post_String += "Position_Order=" + Position_Order + "";
  }
  var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      var Res_Text = this.responseText;
      var Output_HTML = Res_Text;
      document.getElementById("Return_WIFI_Info").innerHTML = Output_HTML;
    }
  };
  xhttp.open("POST", "/Set_WIFI_SSID_Password", true);
  xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhttp.send(Post_String);

}
